package com.xs.project.service.impl;

import com.xs.project.common.Result;
import com.xs.project.enums.ResourceType;
import com.xs.project.mapper.CourseResourceMapper;
import com.xs.project.pojo.vo.CourseResourceRequest;
import com.xs.project.pojo.vo.StuDeviceVo;
import com.xs.project.pojo.vo.StudentDeviceRequest;
import com.xs.project.pojo.vo.StudentHomeWorkReq;
import com.xs.project.pojo.vo.resp.CourseResourceResp;
import com.xs.project.pojo.vo.resp.StudentCourseDeviceResp;
import com.xs.project.pojo.vo.resp.StudentHomeWork;
import com.xs.project.service.ICourseResourceService;
import com.xs.project.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 等风来
 * @since 2024-08-13
 */
@Service
public class CourseResourceServiceImpl implements ICourseResourceService {

    @Resource
    private CourseResourceMapper courseResourceMapper;
    @Override
    public Result<List<CourseResourceResp>> selectResourceList(CourseResourceRequest request) {
        if (StringUtils.isEmpty(request.getCourseName())){
            return null;
        }
        List<CourseResourceResp> respList = courseResourceMapper.selectResource(request);
        for (CourseResourceResp resp : respList) {
            String desc = ResourceType.getDesc(resp.getResourceType());
            resp.setResourceTypeDesc(desc);
        }
        if (request.getPageNum() <= 0){
            request.setPageNum(1);
        }
        long skipSize = (long) request.getPageSize() * (request.getPageNum() - 1); // 计算需要跳过的元素数量

        List<CourseResourceResp> pageRes = respList.stream()
                .skip(skipSize)
                .limit(request.getPageSize())
                .collect(Collectors.toList());
        return Result.success(pageRes,respList.size());
    }

    @Override
    public Result<List<StudentCourseDeviceResp>> selectStudentDevices(StudentDeviceRequest request) {
        List<StudentCourseDeviceResp> devices = courseResourceMapper.selectStudentDevices(request);
        if (CollectionUtils.isEmpty(devices)){
            return Result.success(new ArrayList<>());
        }
        for (StudentCourseDeviceResp device : devices) {
            device.setCreateTimeStr(DateUtil.getDateStr(device.getCreateTime()));
        }

        if (request.getPageNum() <= 0){
            request.setPageNum(1);
        }
        long skipSize = (long) request.getPageSize() * (request.getPageNum() - 1); // 计算需要跳过的元素数量

        List<StudentCourseDeviceResp> pageRes = devices.stream()
                .skip(skipSize)
                .limit(request.getPageSize())
                .collect(Collectors.toList());
        for (StudentCourseDeviceResp pageRe : pageRes) {
            pageRe.setCreateTimeStr(DateUtil.getDateStr(pageRe.getCreateTime()));
            if ("0" .equals(pageRe.getHandleStatus())){
                pageRe.setHandleStatusStr("未处理");
            }else{
                pageRe.setHandleStatusStr("已处理");
            }
        }
        return Result.success(pageRes,devices.size());
    }

    @Override
    public Result<List<StudentHomeWork>> selectStudentHomeWork(StudentHomeWorkReq request) {
        List<StudentHomeWork> studentHomeWorks = courseResourceMapper.selectStudentHomeWork(request);
        if (CollectionUtils.isEmpty(studentHomeWorks)){
            return Result.success(new ArrayList<>());
        }
        if (request.getPageNum() <= 0){
            request.setPageNum(1);
        }
        long skipSize = (long) request.getPageSize() * (request.getPageNum() - 1); // 计算需要跳过的元素数量

        List<StudentHomeWork> pageRes = studentHomeWorks.stream()
                .skip(skipSize)
                .limit(request.getPageSize())
                .collect(Collectors.toList());
        for (StudentHomeWork pageRe : pageRes) {
            if ("0".equals(pageRe.getStatus())){
                pageRe.setStatusStr("未处理");
            }else {
                pageRe.setStatusStr("已处理");
            }
        }
        return Result.success(pageRes,studentHomeWorks.size());
    }

    @Override
    public Result<Boolean> handleStudentHomeWork(Integer id, String homeWorkPoint) {
        if (StringUtils.isEmpty(homeWorkPoint) || !StringUtils.isNumeric(homeWorkPoint)){
            return Result.fail("请输入正确的分数", false);
        }
        courseResourceMapper.handleStudentHomeWork(id,homeWorkPoint,"1");
        return Result.success(true);
    }

    @Override
    public Boolean handleDeviceList(Integer id, String teacherDesc) {
        courseResourceMapper.handleDeviceList(id,teacherDesc);
        return true;
    }

    @Override
    public Boolean insertDeviceList(StuDeviceVo request) {
        request.setStatus("0");
        courseResourceMapper.insertDeviceList(request);
        return true;
    }

    @Override
    public Result<Boolean> insertStudentHomeWork(StudentHomeWorkReq request) {
        courseResourceMapper.insertStudentHomeWork(request);
        return Result.success(true);
    }

    @Override
    public Boolean insertResourceList(CourseResourceResp request) {
        if (StringUtils.isEmpty(request.getResourceName()) ||
                StringUtils.isEmpty(request.getResourcePath()) ||
                request.getResourceType() == null){
            return false;
        }
        courseResourceMapper.insertResourceList(request);
        return true;
    }

    @Override
    public Boolean updateResourceList(CourseResourceResp request) {
        if (StringUtils.isEmpty(request.getResourceName()) ||
        StringUtils.isEmpty(request.getResourcePath()) ||
                request.getResourceType() == null){
            return false;
        }
        courseResourceMapper.updateResourceList(request);
        return true;
    }
}
